package com.crm.controller;

import java.sql.Array;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.crm.model.pojo.SalesOpportunity;
import com.crm.services.CustomerServices;
import com.crm.services.DictionaryServices;
import com.crm.services.SaleTrackingLogServices;
import com.crm.services.SalesOpportunityServices;
import com.crm.services.UserServices;

@Controller
@RequestMapping("/sales")
public class SalesController {

	@Autowired
	private SalesOpportunityServices ss;
	@Autowired
	private DictionaryServices ds;
	@Autowired
	private CustomerServices cs;
	@Autowired
	private UserServices us;
	@Autowired	
	private SaleTrackingLogServices stss;
	
	/**
	 * 销售机会页面的初始化
	 * @return
	 */
		
		@RequestMapping("/saleOpp")
		//@ResponseBody
		public ModelAndView findAllsaleOpportunity() {
			String[] DId=new String[] {"全部数据"};
			JSONArray array=ss.ListTransJSONArray(0,4,DId);//分页查询所有	
			ModelAndView mv=new ModelAndView("sales/saleOpp");
			mv.addObject("salesCount", ss.queryByCount());
			mv.addObject("salesOpp",array);
		//	mv.addObject("AllPageNumber", ss.AllPageNumber(0, 4));
			System.out.println("销售机会表");
			return mv;
		}
		
		/**
		 * 销售机会的分页查询
		 * @param <E>
		 * @param pageNumberl
		 * @param pageCentent
		 * @return
		 */
		@RequestMapping("/salePageable")
		@ResponseBody
		public  JSONObject salePageable(Integer pageNumber,Integer pageCentent, String[] DId) {
			System.out.println("pageNumber:"+pageNumber);
			System.out.println("pageCentent:"+pageCentent);
			System.out.println("DId的长度:"+DId.length);		
			int a=0;
			JSONObject json=new JSONObject();
			for (String string : DId) {
				System.out.println("DId"+a+":"+string);		 
				a+=1;
			}
			json.put("saleOpp",ss.ListTransJSONArray(pageNumber-1,pageCentent,DId));
			json.put("salesCount", ss.queryByCount());
			return json;			
		}
		
		/***
		 * 销售机会新增时的数据字典信息
		 * @return
		 */
		@RequestMapping("/saleAddDictionaryInfo")
		@ResponseBody
		public JSONArray saleAddDictionaryInfo() {	
			System.out.println("saleAddDictionaryInfo");
			JSONArray array=new JSONArray();
			// array.add(ds.ListTransJSONArray(ds.findByDictParentid(1286)));//销售状态
			 array.add(ds.ListTransJSONArray(ds.findByDictParentid(1292)));//可能性
			 array.add(ds.ListTransJSONArray(ds.findByDictParentid(1304)));//来源
			 array.add(ds.ListTransJSONArray(ds.findByDictParentid(1323))); //销售阶段（1323）
			 array.add(ds.ListTransJSONArray(ds.findByDictParentid(1420)));//销售机会类型(1420)
			return array;			
		}
		
		/**
		 *外表，通过模糊查询对应的负责人名称
		 * @return
		 */
		@RequestMapping("/abroadsaleAddUnameInfo")
		@ResponseBody
		public JSONArray abroadsaleAddUnameInfo(String uName) {	
			System.out.println("abroadsaleAddUnameInfo");
			if(uName==null) {
				System.out.println(cs.findAll());
				return us.ListTransJSONArray(us.findAll());
			}else {
				return us.ListTransJSONArray(us.findByUNameLike(uName));
			}		
		}
		
		/**
		 *内表，通过多条件+模糊查询对应的负责人名称
		 * @return
		 */
		@RequestMapping("/withinsaleAddUnameInfo")
		@ResponseBody
		public JSONArray withinsaleAddUnameInfo(String[] DId) {	
			System.out.println("withinsaleAddUnameInfo");
				return ss.ListTransJSONArray2(ss.queryByAll(0, 5, DId));
		
		}
		/**
		 *内表，通过多条件+模糊查询对应的机会主题名称
		 * @return
		 */
		@RequestMapping("/withinSaleFindByTheme")
		@ResponseBody
		public JSONArray withinSaleFindByTheme(String[] DId) {	
			System.out.println("withinsaleAddUnameInfo");
				return ss.ListTransJSONArray3(ss.queryByAll(0, 5, DId));
		
		}
		
		
		/**
		 *新增 销售机会
		 * @return
		 */
		@RequestMapping("/Addsale")
		@ResponseBody
		public int Addsale(@ModelAttribute("sale") SalesOpportunity sale) {	
			System.out.println("Addsale:"+sale.getSalUpdatetime());
			return ss.save(sale);			
		}
		/**
		 *查找单个销售机会
		 * @return
		 */
		@RequestMapping("/findOneSale")
		@ResponseBody
		public JSONArray findOneSale(Integer salId) {	
			System.out.println("findOneSale"+salId);
			JSONArray array =new JSONArray();
			array.add(ss.findBySalId(salId));//查询单个销售机会
			JSONArray a=stss.queryBySalId(salId);

			if(a!=null||a.size()!=0) {
				System.out.println("日志长度:"+a.size());
				array.add(a);//查询这个销售机会的跟踪日志					
			}
			
			return array;			
		}
		
		/**
		 *删除单个销售机会
		 * @return
		 */
		@RequestMapping("/Delete")
		@ResponseBody
		public int Delete(Integer salId) {	
			System.out.println("Delete:"+salId);
			ss.Delete(salId);
			return 1;			
		}
		/**
		 *批量逻辑删除销售机会
		 * @return
		 */
		@RequestMapping("/DeleteAll")
		@ResponseBody
		public int DeleteAll(Integer[] salId) {	
			System.out.println("Delete:"+salId);
			//ss.DeleteAll(salId);
			return 1;			
		}		
		
		/**
		 * 外表,通过模糊查询对应的客户名称
		 */
		@RequestMapping("/abroadsaleCustName")
		@ResponseBody
		public JSONArray saleCustName(String custName) {
			System.out.println("外表，触发销售机会对应客户字段的模糊查询:"+custName);
			if(custName==null) {
				System.out.println(cs.findAll());
				return cs.ListTransJSONArray(cs.findAll());
			}else {
				return cs.ListTransJSONArray(cs.findByCustNameLike(custName));
			}		
		}
		/**
		 * 内表,通过多条件+模糊查询对应的客户名称
		 */
		@RequestMapping("/withinsaleCustName")
		@ResponseBody
		public JSONArray saleCustName(String[] DId) {
			System.out.println("内表，触发销售机会对应客户字段的模糊查询:"+DId);
			System.out.println("DId的长度:"+DId.length);	
				return ss.ListTransJSONArray1(ss.queryByAll(0, 5, DId));		   		
		}		
		/**
		 * 确认销售机会是否重复
		 */
		@RequestMapping("/findBySalTheme")
		@ResponseBody
		public Integer findBySalTheme(String salTheme) {
			System.out.println("触发确认销售机会是否重复:"+salTheme);			

				return ss.findBySalTheme(salTheme);		
		}
		/**
		 * 查询前台页面的显示字段
		 * @return
		 */
		@RequestMapping("/showInfo")
		@ResponseBody
		public JSONObject showSalesSetting(Integer userId) {
			System.out.println("当前用户编号:"+userId);
			
			return ss.showSalesSetting(userId);
			
		}
		
		/**
		 * 修改前台页面的显示字段
		 */
		@RequestMapping("/updateInfo")
		@ResponseBody
		public void updateSalesSetting(String[] Info) {
			System.out.println("当前要保存的数据显示数据:"+Info);
			
			for (String string : Info) {
				System.out.println(string);
			}
			ss.saveInfo(Info);
		}
		
		/**
		 * 这里是查询单个销售机会信息到ModelAndView然后跳转到销售视图界面的方法
		 * @param salId
		 * @return
		 */
		@GetMapping("/saleView")
		public ModelAndView saleView(Integer salId) {
			System.out.println("saleView跳转:"+salId);
			ModelAndView mv=new ModelAndView("/sales/saleView");
			JSONArray array =new JSONArray();
			array.add(ss.findBySalId(salId));//查询单个销售机会
			JSONArray a=stss.queryBySalId(salId);
			if(a!=null||a.size()!=0) {
				System.out.println("日志长度:"+a.size());
				array.add(a);//查询这个销售机会的跟踪日志					
			}
			mv.addObject("sales", array);
			return mv;
			
		}
		

		
	/*	//solution
		@RequestMapping("/solution")
		public void findAllsolution() {
			System.out.println("解决方案");
		}
		
		@RequestMapping("/competitor")
		public void findAllcompetitor() {
			System.out.println("竞争对手");
		}
		 
		@RequestMapping("/QuotationRecord")
		public void findAllQuotationRecord() {
			System.out.println("报价记录");
		}
		
		@RequestMapping("/QuotationDetails")
		public void findAllQuotationDetails() {
			System.out.println("报价明细");
		}*/
		


}
